#include "keyboard_queue.h"

void queueInit(volatile Queue *q) {
	q->in = q->out = 0;
	q->cnt = 0;
	q->size = 16;
}

bool_t queuePut(volatile Queue *q, char c) {
	if (queueFull(q))
		return false;
	
  q->buf[q->in] = c;
  
  /* incrementa para a proxima posicao */
  ++q->in;

  /* incrementar o numero de elementos da queue */
  ++q->cnt;
  
  if(q->in == q->size)
    q->in = 0;
	
	return true;
}

char queueGet(volatile Queue *q) {
	if (queueEmpty(q))
		return -1;
    
  char c = q->buf[q->out];
  ++q->out;
  --q->cnt;
  if(q->out == q->size)
    q->out = 0;
    
  return c;
}

bool_t queueEmpty(volatile Queue *q) {
	return (q->cnt == 0);
}

bool_t queueFull(volatile Queue *q) {
	return (q->cnt == q->size);
}
